package net.aiion.weave.impl.naming;

import net.aiion.weave.spi.naming.Context;
import net.aiion.weave.spi.naming.Contextual;
import net.aiion.weave.spi.naming.GlobalContext;
import net.aiion.weave.spi.naming.SessionContext;
import net.aiion.weave.spi.naming.annotations.GlobalScoped;

public class GlobalContextImpl extends ContextImpl implements GlobalContext
{
    private static final String GLOBAL_CONTEXT_NAME = GlobalContext.class.getName();

    public GlobalContextImpl() {
        super(GlobalScoped.class);
    }

    @Override
    public SessionContext createSessionContext() {
        return new SessionContextImpl(this);
    }
    
    @Override
    public SessionContext lookupSessionContext(final String aSessionContextId) {
        final Context context = getChildContextById(aSessionContextId);
        if (!(context instanceof SessionContext)) {
            return createSessionContext();
        }

        return (SessionContext) context;
    }

    @Override
    protected Contextual lookupContext(final String bindingName) {
        if (GLOBAL_CONTEXT_NAME.equals(bindingName)) {
            return bind(bindingName, this);
        }

        return super.lookupContext(bindingName);
    }
}
